package com.kaisu.blog.web.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * @author kaisu
 */
@Schema(description = "通用返回结果")
@Data
public class Result<T> {

    /**
     * 结果状态码
     */
    @Schema(description = "结果状态码")
    private int code;

    /**
     * 响应信息
     */
    @Schema(description = "响应信息")
    private String message;

    /**
     * 数据
     */
    @Schema(description = "数据")
    private T data;

    /**
     * 接口请求时间
     */
    @Schema(description = "接口请求时间")
    private long timestamp;

    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(int code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> fail(String msg) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.FAIL.getCode());
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMsg());
        return result;
    }
}
